package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.bo.ErpCollectAbsentSkuBo;
import com.erp.gather.domain.vo.ErpCollectAbsentSkuVo;
import com.erp.gather.mapper.ErpCollectAbsentSkuMapper;
import com.erp.gather.service.IErpCollectAbsentSkuService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.gather.api.domain.ErpCollectAbsentSku;
import com.ruoyi.order.api.domain.ErpSellerOrder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * erp不存在的skuService业务层处理
 *
 * @author erp
 * @date 2023-05-18
 */
@RequiredArgsConstructor
@Service
public class ErpCollectAbsentSkuServiceImpl implements IErpCollectAbsentSkuService {

    private final ErpCollectAbsentSkuMapper baseMapper;
//    private final ErpSellerOrderMapper erpSellerOrderMapper;
//

    /**
     * 根据条件查询不存在的sku
     * @param skuCode
     * @return
     */
    @Override
    public ErpCollectAbsentSku getBySkuCode(String skuCode){
        return baseMapper.selectOne(new LambdaQueryWrapper<ErpCollectAbsentSku>().eq(ErpCollectAbsentSku::getSkuCode, skuCode));
    }
    /**
     * 查询erp不存在的sku
     */
    @Override
    public ErpCollectAbsentSkuVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询erp不存在的sku列表
     */
    @Override
    public TableDataInfo<ErpCollectAbsentSku> queryPageList(ErpCollectAbsentSkuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpCollectAbsentSku> lqw = buildQueryWrapper(bo);
        Page<ErpCollectAbsentSku> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询erp不存在的sku列表
     */
    @Override
    public List<ErpCollectAbsentSkuVo> queryList(ErpCollectAbsentSkuBo bo) {
        LambdaQueryWrapper<ErpCollectAbsentSku> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpCollectAbsentSku> buildQueryWrapper(ErpCollectAbsentSkuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpCollectAbsentSku> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), ErpCollectAbsentSku::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuCode()), ErpCollectAbsentSku::getSkuCode, bo.getSkuCode());
        lqw.eq(StringUtils.isNotBlank(bo.getCollectLink()), ErpCollectAbsentSku::getCollectLink, bo.getCollectLink());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ErpCollectAbsentSku::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpCollectAbsentSku::getRevision, bo.getRevision());
        lqw.eq(StringUtils.isNotBlank(bo.getChildOrderId()), ErpCollectAbsentSku::getChildOrderId, bo.getChildOrderId());
        lqw.orderByAsc(ErpCollectAbsentSku::getCreateTime);
        return lqw;
    }

    /**
     * 新增erp不存在的sku
     */
    @Override
    public Boolean insertByBo(ErpCollectAbsentSkuBo bo) {
        ErpCollectAbsentSku add = BeanUtil.toBean(bo, ErpCollectAbsentSku.class);
        validEntityBeforeSave(add);
        // 根据orderId查询,存在修改,不存在新增
        String orderId = bo.getOrderId();
        ErpCollectAbsentSku absentSku = baseMapper.selectOne(new LambdaQueryWrapper<ErpCollectAbsentSku>().eq(ErpCollectAbsentSku::getOrderId, orderId.toString()));
        if (ObjectUtils.isNotEmpty(absentSku)) {
            absentSku.setCollectLink(bo.getCollectLink());
            return baseMapper.updateById(absentSku) > 0;
        }else {
            return baseMapper.insert(add) > 0;
        }
    }

//    /**
//     * 修改erp不存在的sku
//     */
//    @Override
//    public Boolean updateByBo(ErpCollectAbsentSkuBo bo) {
//        ErpCollectAbsentSku update = BeanUtil.toBean(bo, ErpCollectAbsentSku.class);
//        validEntityBeforeSave(update);
//        ErpSellerOrder order = erpSellerOrderMapper.selectOne(
//          new LambdaQueryWrapper<ErpSellerOrder>().eq(ErpSellerOrder::getOrderId,bo.getOrderId())
//        );
//        return baseMapper.updateById(update) > 0;
//    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCollectAbsentSku entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除erp不存在的sku
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
